import {Component, OnInit} from "@angular/core";
import {LoginService} from "../service/login.service";
import {FrontUser} from "../entity/User";
import {UserService} from "../service/user.service";
import {CommonLogin} from "../common/CommonLogin";
import {Router} from "@angular/router";
@Component({
  selector: 'user-regist',
  templateUrl: './signup.html',
})
export class RegistComponent extends CommonLogin implements  OnInit{

  public agreeSelected:boolean =false;

  constructor(public  loginservice:LoginService,public router:Router){
    super();
  }
  user:FrontUser=new FrontUser();
  ngOnInit(): void {
    this.HiddenLeftProgram();
    this.generateValideCode();
  }
  onSubmit( user:FrontUser) {

      if (user.name == null) {
        $.jBox.tip("请输入姓名");

      }
      else if (user.email == null) {
        $.jBox.tip("请输入邮箱");
      }
      else if (user.password == null) {
        $.jBox.tip("请输入密码");
      }
      else if (!this.IsEmail(user.email)) {
        $.jBox.tip("请输入正确的邮箱");
      } else if (this.IsPas(user.password)) {
        $.jBox.tip("密码至少为6位");
      } else if (user.code == null) {
        $.jBox.tip("请输入验证码");
      } else if (this.agreeSelected == false) {
        $.jBox.tip("同意协议后,才能注册!");
      } else {
        this.loginservice.RegisterUser(user);
      }

  }


  private generateValideCode() {
    let oldRandom = this.user.random;
    this.user.random = Date.now().toString(36)
      +(Math.floor(Math.random() * 100000000) + 100000000).toString();
    let randomRequest = this.userIp + "/servlet/validateCodeServlet?" + Math.random() + "&random=" + this.user.random+"&oldRandom="+oldRandom;
    document.getElementById("codes").setAttribute("src", randomRequest);
  }
  public changeValideCode(){
    this.generateValideCode();
  }

  public login(){
    this.router.navigate(['/isLogin']);
  }
  public IsPas(pas:string){
    if(pas.length<6){
      return true;
    }else {
      return false;
    }
  }
}
